﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using BusinessManagementStudio.Models.BusinessObject.Services;

namespace BusinessManagementStudio.Controllers
{
    public class UserController : Controller
    {
        private UserService userService = new UserService();
        private User user = new User();

        //
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PasswordReset()
        {
            return View();
        }

        public ActionResult NonAdmin()
        {
            if(TempData["UserObj"] != null)
                user = (User)TempData["UserObj"];

            return View(user);
        }

        public ActionResult Admin()
        {
            if (TempData["UserObj"] != null)
                user = (User)TempData["UserObj"];

            return View(user);
        }

        public ActionResult UserLoginFailure()
        {
            String alertMessage = "Either your login name or login password is(are) incorrect!";

            ViewBag.alertMessage = alertMessage;

            return View();
        }

        public ActionResult UserNameOrPwdLoginFailure()
        {
            String alertMessage = "Your login name or Your password should not be blank!";

            ViewBag.alertMessage = alertMessage;

            return View();
        }

        [HttpPost]
        public ActionResult MultipleCommand(String command, String UserName, String Password, String ConfirmedPassword, String UserType)
        {
            if (command == "Register")
                return RedirectToAction("Register");
            else if (command == "Login")
            {
                #region LogIn
                if (UserName != "" && Password != "")
                {
                    bool isCorrectUser = userService.IsCorrectUser(UserName, Password);

                    if (isCorrectUser == true)
                    {
                        User ExistingUser = userService.GetUserDetail(UserName, Password);
                        TempData["UserObj"] = ExistingUser;

                        if (ExistingUser.UserType == "Administrator")
                            return RedirectToAction("Admin");
                        else if (ExistingUser.UserType == "User")
                            return RedirectToAction("NonAdmin");
                    }
                    else
                    {
                        return RedirectToAction("UserLoginFailure");
                    }
                }
                else if (UserName == "" || Password == "")
                    return RedirectToAction("UserNameOrPwdLoginFailure");
                #endregion
            }
            else if (command == "Cancel")
                return RedirectToAction("Index", "Home");
            else if (command == "Create")
            {
                #region Create
                if (Password == ConfirmedPassword && UserName != "" && Password != "" && ConfirmedPassword != "")
                {
                    UserRegistration UserReg = new UserRegistration();
                    UserReg.UserName = UserName;
                    UserReg.Password = Password;
                    UserReg.ConfirmedPassword = ConfirmedPassword;
                    UserReg.UserType = UserType;

                    // Check the user account is existed or not
                    // If the user account is not existed in the database, then create an account into database
                    // Otherwise, the warning message is shown
                    bool isExistignAcct = userService.IsExistingUserAcct(UserReg);

                    if (isExistignAcct == false)
                    {
                        User NewUser = new User();
                        NewUser.UserName = UserReg.UserName;
                        NewUser.UserType = UserReg.UserType;
                        NewUser.Password = UserReg.Password;
                        NewUser.CreateDate = DateTime.Today;

                        userService.Create(NewUser);

                        return RedirectToAction("Index", "Home");
                    }
                }
                #endregion
            }

            return View();
        }

        public ActionResult Register()
        {
            UserType uTypeNames = new UserType();
            uTypeNames.UserTypeName = "User";

            return View(uTypeNames);
        }
    }
}
